package com.tinycloud.miracll.product.job;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.tinycloud.miracll.product.domain.*;
import com.tinycloud.miracll.product.domain.json.ScadaTagValues;
import com.tinycloud.miracll.product.service.*;
import com.tinycloud.miracll.product.util.BasicAuthUtil;
import com.tinycloud.miracll.product.util.ScadaUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("scadaTask")
public class SCADATask {

    @Autowired
    IErpProductProdOrderService erpProductProdOrderService;
    @Autowired
    IErpProductProcessCtrlService erpProductProcessCtrlService;
    @Autowired
    IErpProductQCMProcessCtrlMainService erpProductQCMProcessCtrlMainService;
    @Autowired
    IErpProductQCMProcessCtrlDetailService erpProductQCMProcessCtrlDetailService;

    @Autowired
    IProductGatherService productGatherService;
    @Autowired
    IProductInfoService productInfoService;

    @Autowired
    IDeviationDataDetailService deviationDataDetailService;
    @Autowired
    IDeviationDataMainService deviationDataMainService;

    @Autowired
    IAuxiSysWeightService auxiSysWeightService;

    @Autowired
    public BasicAuthUtil basicAuthUtil;



    @Autowired
    public IProductKeyProcessDeviateAlarmService productKeyProcessDeviateAlarmService;


    private static final Logger logger = LoggerFactory.getLogger(SCADATask.class);


    public void getProductData() {
        ErpProductProdOrder erpProductProdOrder = new ErpProductProdOrder();
        erpProductProdOrder.setScadaStatus("生产中");
        erpProductProdOrder.setOrderStatus("投产");
        List<ErpProductProdOrder> erpProductProdOrderList = erpProductProdOrderService.selectErpProductProdOrderList(erpProductProdOrder);
        System.out.println("getProductData......."+erpProductProdOrderList.size());
        //循环再生产的订单
        logger.info("productOrder size is:  "+erpProductProdOrderList.size());
        for (ErpProductProdOrder erpProductProdOrderTem:erpProductProdOrderList) {

            ErpProductProcessCtrl erpProductProcessCtrl = new ErpProductProcessCtrl();
            String orderNum = erpProductProdOrderTem.getOrderNum();
            String batchNum = erpProductProdOrderTem.getBatchNum();
            String prodLineCode = erpProductProdOrderTem.getProdLineCode();
            String QCNum = erpProductProdOrderTem.getQcNum();
            String gatherID = UUID.randomUUID().toString();
            ProductGather productGather = new ProductGather();
            productGather.setId(gatherID);
            productGather.setOrderNum(orderNum);
            productGather.setBatchNum(batchNum);
            productGather.setProdLineCode(prodLineCode);
            productGather.setQcNum(QCNum);
            productGather.setAcquisitionTime(DateTime.now());
            this.saveOrderGather(productGather);
            if (StrUtil.isNotBlank(prodLineCode)) {
                erpProductProcessCtrl.setProdLineCode(prodLineCode);
                List<ErpProductProcessCtrl> erpProductProcessCtrlList = new ArrayList<ErpProductProcessCtrl>();
                erpProductProcessCtrlList = erpProductProcessCtrlService.selectErpProductProcessCtrlList(erpProductProcessCtrl);
                //循环产线对应工艺点
                logger.info("productProcessCtrlList size is:  "+erpProductProcessCtrlList.size());
                for (ErpProductProcessCtrl erpProductProcessCtrlTem: erpProductProcessCtrlList) {

                    String measurePoint = erpProductProcessCtrlTem.getMeasurePoint();

                    String codeNum = erpProductProcessCtrlTem.getCode();
                    if (StrUtil.isBlank(measurePoint)){
                        logger.info(erpProductProcessCtrlTem.getRemark()+"  measurePoint is null....");
                        continue;
                    }else {

                        ScadaTagValues scadaTagValue = ScadaUtil.getScadaVaules(measurePoint);
                        logger.info("SCADA point is :  "+scadaTagValue.getName()+"   value is:  " + scadaTagValue.getValue());
                        ErpProductQCMProcessCtrlMain erpProductQCMProcessCtrlMain = new ErpProductQCMProcessCtrlMain();
                        erpProductQCMProcessCtrlMain.setQcNum(QCNum);
                        erpProductQCMProcessCtrlMain.setProdLineCode(prodLineCode);
                        List<ErpProductQCMProcessCtrlMain> erpProductQCMProcessCtrlMainList =  erpProductQCMProcessCtrlMainService.selectErpProductQCMProcessCtrlMainList(erpProductQCMProcessCtrlMain);
                        //获取对应品管号点设置数据
                        ErpProductQCMProcessCtrlDetail erpProductQCMProcessCtrlDetailResult = new ErpProductQCMProcessCtrlDetail();
                        logger.info("erpProductQCMProcessCtrlMainList  size is:  "+erpProductQCMProcessCtrlMainList.size());
                        if (0!=erpProductQCMProcessCtrlMainList.size()) {
                            ErpProductQCMProcessCtrlMain erpProductQCMProcessCtrlMainResult = erpProductQCMProcessCtrlMainList.get(0);
                            ErpProductQCMProcessCtrlDetail erpProductQCMProcessCtrlDetail = new ErpProductQCMProcessCtrlDetail();
                            String mainRCID = erpProductQCMProcessCtrlMainResult.getExcelServerRCID();
                            erpProductQCMProcessCtrlDetail.setExcelServerRCID(mainRCID);
                            erpProductQCMProcessCtrlDetail.setCode(codeNum);
                            List<ErpProductQCMProcessCtrlDetail> erpProductQCMProcessCtrlDetailList = erpProductQCMProcessCtrlDetailService.selectErpProductQCMProcessCtrlDetailList(erpProductQCMProcessCtrlDetail);
                            logger.info("erpProductQCMProcessCtrlDetailList  size is:  "+erpProductQCMProcessCtrlDetailList.size());
                            if (0!=erpProductQCMProcessCtrlDetailList.size()) {
                                erpProductQCMProcessCtrlDetailResult = erpProductQCMProcessCtrlDetailList.get(0);
                                ProductInfo productInfo = new ProductInfo();
                                String infoID = UUID.randomUUID().toString();
                                String issuePoint = erpProductQCMProcessCtrlDetailResult.getCode();
                                Double issueValue = erpProductQCMProcessCtrlDetailResult.getLimit();
                                Double upLimit = erpProductQCMProcessCtrlDetailResult.getUpLimit();
                                Double lowLimit = erpProductQCMProcessCtrlDetailResult.getLowLimit();
                                Double upUpLimit = erpProductQCMProcessCtrlDetailResult.getUpUpLimit();
                                Double lowLowLimit =  erpProductQCMProcessCtrlDetailResult.getLowLowLimit();
                                String keyProcessFlag = erpProductQCMProcessCtrlDetailResult.getKeyProcessPoint();
                                String recordFlag = erpProductQCMProcessCtrlDetailResult.getRecordFlag();
                                String remark = erpProductQCMProcessCtrlDetailResult.getRemark();
                                Double measureValue = scadaTagValue.getValue();

                                ScadaTagValues scadaIssueTagValue = ScadaUtil.getScadaVaules(issuePoint);
                                Double scadaIssueValue = scadaIssueTagValue.getValue();

                                productInfo.setId(infoID);
                                productInfo.setIssuePoint(issuePoint);
                                productInfo.setIssueValue(issueValue);
                                productInfo.setMeasurePoint(measurePoint);
                                productInfo.setMeasureValue(measureValue);
                                productInfo.setUpLimit(upLimit);
                                productInfo.setLowLimit(lowLimit);
                                productInfo.setUpUpLimit(upUpLimit);
                                productInfo.setLowLowLimit(lowLowLimit);
                                productInfo.setProductorderID(gatherID);
                                productInfo.setRemark(remark);
                                productInfo.setScadaIssueValue(scadaIssueValue);
                                if ("是".equals(keyProcessFlag)){
                                    productInfo.setKeyProcessFlag("Y");
                                } else if ("否".equals(keyProcessFlag)) {
                                    productInfo.setKeyProcessFlag("N");
                                }
                                if ("是".equals(recordFlag)){
                                    productInfo.setRecordFlag("Y");
                                } else if ("否".equals(recordFlag)) {
                                    productInfo.setRecordFlag("N");
                                }

                                if("Y".equals(productInfo.getKeyProcessFlag())) {
                                    this.saveOrderGatherDetail(productInfo);
                                }

                            }


                        }


                    }


                }

            }

        }


    }

    public void cleanScadaAlarmData() {

        productKeyProcessDeviateAlarmService.cleanScadaAlarmData();

    }

    public void productKeyProcessConfERP2Scada() {

        productKeyProcessDeviateAlarmService.productKeyProcessConfERP2Scada();

    }

    public void auxiSysWeightDadaClean() throws ParseException {

        auxiSysWeightService.getEndWeightDate();

    }


/*
    public void genDeviationData ()  {
        ErpProductProdOrder erpProductProdOrder = new ErpProductProdOrder();
        erpProductProdOrder.setScadaStatus("生产中");
        erpProductProdOrder.setOrderStatus("投产");
        List<ErpProductProdOrder> erpProductProdOrderList = erpProductProdOrderService.selectErpProductProdOrderList(erpProductProdOrder);
        for (ErpProductProdOrder erpProductProdOrderTem : erpProductProdOrderList) {
            ErpProductOrderGather erpProductOrderGather = new ErpProductOrderGather();
            erpProductOrderGather.setOrderNum(erpProductProdOrderTem.getOrderNum());
            List<ErpProductOrderGather> erpProductOrderGatherList = productGatherService.selectProductGatherList(erpProductOrderGather);
            if (0!=erpProductOrderGatherList.size()) {
                ErpProductOrderGather erpProductOrderGatherResult = erpProductOrderGatherList.get(0);
                String gatherID = erpProductOrderGatherResult.getId();
                ErpOrderProducInfo erpOrderProducInfo = new ErpOrderProducInfo();
                erpOrderProducInfo.setProductorderid(gatherID);
                List<ErpOrderProducInfo> erpOrderProducInfoList = productInfoService.selectProductInfoList(erpOrderProducInfo);
                String mainID = UUID.randomUUID().toString();
                DeviationDataMain deviationDataMain = new DeviationDataMain();
                if (0!=erpOrderProducInfoList.size()) {
                    deviationDataMain.setOrderNum(erpProductOrderGatherResult.getOrderNum());
                    deviationDataMain.setQcNum(erpProductOrderGatherResult.getQcNum());
                    deviationDataMain.setBatchNum(erpProductOrderGatherResult.getBatchNum());
                    deviationDataMain.setProdLineCode(erpProductOrderGatherResult.getProdLineCode());
                    deviationDataMain.setAcquisitionTime(erpProductOrderGatherResult.getAcquisitionTime());
                    deviationDataMain.setId(mainID);
                    deviationDataMain.setPushFlag("N");
                    deviationDataMain.setRemark(erpProductOrderGatherResult.getRemark());
//                    deviationDataMainService.insertDeviationDataMain(deviationDataMain);
                }
                Boolean insertMainFlag = false;
                for (ErpOrderProducInfo erpOrderProducInfoTem: erpOrderProducInfoList) {
                    Double measureValue = erpOrderProducInfoTem.getMeasureValue();
                    Double lowLimit = erpOrderProducInfoTem.getLowLimit();
                    Double upLimit = erpOrderProducInfoTem.getUpLimit();
                    boolean recordFlag = false;
                    if (null!=lowLimit&&measureValue.doubleValue()<lowLimit.doubleValue()) {
                        recordFlag = true;
                    }
                    if (null!=upLimit&&measureValue.doubleValue()>upLimit.doubleValue()) {
                        recordFlag = true;
                    }
                    if (recordFlag&&!insertMainFlag) {
                        deviationDataMainService.insertDeviationDataMain(deviationDataMain);
                        insertMainFlag = true;

                    }
                    if (recordFlag) {
                        DeviationDataDetail deviationDataDetail = new DeviationDataDetail();
                        deviationDataDetail.setId(UUID.randomUUID().toString());
                        deviationDataDetail.setProdInfoID(mainID);
                        deviationDataDetail.setIssuePoint(erpOrderProducInfoTem.getIssuePoint());
                        deviationDataDetail.setMeasurePoint(erpOrderProducInfoTem.getMeasurePoint());
                        deviationDataDetail.setMeasureValue(erpOrderProducInfoTem.getMeasureValue());
                        deviationDataDetail.setIssueValue(erpOrderProducInfoTem.getIssueValue());
                        deviationDataDetail.setUpLimit(erpOrderProducInfoTem.getUpLimit());
                        deviationDataDetail.setLowLimit(erpOrderProducInfoTem.getLowLimit());
                        deviationDataDetail.setProdInfoID(mainID);
                        deviationDataDetail.setRemark(erpOrderProducInfoTem.getRemark());
                        deviationDataDetailService.insertDeviationDataDetail(deviationDataDetail);


                    }
                    
                }

            }

        }

    }*/

    public void  saveOrderGather(ProductGather productGather) {
        productGatherService.insertProductGather(productGather);
    }

    public void  saveOrderGatherDetail(ProductInfo productInfo ) {
        productInfoService.insertProductInfo(productInfo);
    }



}